<?php

namespace App\Models\Factory\V0;



use App\Helpers\DateUtils;
use App\Helpers\Utils;
use App\Models\AbsModelFactory;
use App\Models\Orm\V0\User;


/**
 * 生成恒信订单号
 */
class UserFactory extends AbsModelFactory
{
    /**
     * 添加用户
     *
     * @param int $action_code
     * @return bool|string
     */
    public static function updateUser($data,$token)
    {
        $result = User::where('token',$token)->update($data);
        return $result ? true : false;
    }


    //发短信插入数据库
    public static function addUser($mobile,$token)
    {
        $user = User::firstOrCreate(
            [
                'register_phone' => $mobile
            ],
            [
                'register_phone' => $mobile,
                'registe_time' => DateUtils::getToday(),
                'created_ip'      => Utils::ipAddress(),
            ]
        );

        return $user? true : false;
    }

    /**
     * 通过手机号查询用户信息
     */
    public static function getUserByMobile($mobile)
    {
        $user = User::where(['register_phone' => $mobile])->first();
        return $user;
    }



      //给用户表增加渠道来源
    public static  function addChannel($userId,$channel)
    {
        $data['channel_nid_register'] = $channel;
        $user = User::where('id',$userId)->update($data);
        return $user ? true : false;
    }


    //给用户表增加渠道来源
    public static  function addChannelLogin($userId,$channel)
    {
        $data['channel_nid'] = $channel;
        $user = User::where('id',$userId)->update($data);
        return $user ? true : false;
    }




    /**
     * 通过手机号查询用户信息
     */
    public static function getUser($mobile)
    {
        $user = User::where(['register_phone' => $mobile])->first();
        if($user)
        {
            if($user->password){
                return false;
            }
            return true;
        }
        return true;
    }




    /**
     * 生成的token保存进用户表
     */
    public static function addUserTokenByMobile($mobile,$token)
    {
        $user = User::where(['register_phone' => $mobile])->update($token);
        return $user;
    }

    /*
   * 修改登录时间
   *
   */
    public static function addUserLassTime($mobile)
    {
        $time['lass_time'] = DateUtils::getToday();
        $user = User::where(['register_phone' => $mobile])->update($time);
        return $user;
    }

    /**
     * 通过token查找用户对象
     *
     * @param $token
     * @return mixed
     */
    public static function getUserByToken($token)
    {
        return User::where('token', $token)->first();
    }

    //通过小微商户号拿到用户id
    public static  function getUserId($xwMerId)
    {
       $user = User::where('xwMerId',$xwMerId)->first();
        return $user ? $user : false;
    }

    //通过身份证拿到用户信息
    public static  function getUserByIdCard($id_card)
    {

        $user = User::where('idcard',$id_card)->first();
        if($user)
        {
            if($user -> xwMerId)
            {
                return true;
            };
        }
        return  false;
    }

    //通过用户id更新用户信息
    public static  function updateUserById($user_id,$data)
    {
        $result = User::where('id',$user_id)->update($data);
        return $result ? true : false;
    }

    //通过身份证号获取用户信息
    public static  function  getUserByIdCards($idCard)
    {
        $result = User::where('idcard',$idCard)->first();

        return $result ? true : false;

    }

}